package net.lab1024.smartadmin.common.weixin;

import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.alibaba.fastjson.JSONObject;
import net.lab1024.smartadmin.module.system.employee.domain.dto.CpUserDTO;
import net.lab1024.smartadmin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.smartadmin.module.weapp.message.MessageDao;
import net.lab1024.smartadmin.module.weapp.message.domain.entity.MessageEntity;
import net.lab1024.smartadmin.module.weapp.orders.domain.entity.OrdersEntity;
import net.lab1024.smartadmin.module.weapp.orderstype.dao.WorkOrderTypeDao;
import net.lab1024.smartadmin.module.weapp.orderstype.domain.vo.WorkOrderTypeVO;
import net.lab1024.smartadmin.module.weapp.users.domain.vo.UsersVO;
import net.lab1024.smartadmin.module.weapp.visitor.VisitorEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AddToMessageQueue {
    @Value("${wx.miniapp.subsriceMessage.NEWNOTICE}")
    private String NEWNOTICE;

    @Value("${wx.miniapp.subsriceMessage.WCNOTICE}")
    private String WCNOTICE;

    @Value("${wx.miniapp.subsriceMessage.HANDNOTICE}")
    private String HANDNOTICE;

    @Autowired
    private MessageDao messageDao;


    public void addMinMessage(VisitorEntity entity, EmployeeEntity em, int messageType){
        this.addMinMessage(entity,em,messageType,"");
    }
    /**
     * 添加推送消息，
     * 根据用户类型添加 小程序订阅消息或者企业微信推送消息

     * @param entity 工单内容主体
     * @param em touser内容
     */
    public void addMinMessage(VisitorEntity entity, EmployeeEntity em,int messageType,String subTitle){

        if(em != null && em.getUserId() != null){

            //发送企业微信消息
            MessageEntity messEntity2 = new MessageEntity();
            if(messageType == 0){
                messEntity2.setTitle("新增访客审核提醒"+subTitle);
            }else if(messageType == 1){
                messEntity2.setTitle("访客信息审核通过提醒"+subTitle);
            }
            else if(messageType == 2){
                messEntity2.setTitle("访客进厂提醒"+subTitle);
            }else if(messageType == 3){
                messEntity2.setTitle("访客出厂提醒"+subTitle);
            }else if(messageType == 4){
                messEntity2.setTitle("访客信息审核未通过提醒"+subTitle);
            }else if(messageType == 5){
                messEntity2.setTitle("访客信息门卫核验未通过提醒"+subTitle);
            }


            messEntity2.setType(messageType);
            messEntity2.setToUser(em.getUserId());
            messEntity2.setPage("pages/visitor/info?id=" + entity.getId());
            Map<String, String> mscpData = new HashMap<>();
            if(messageType >= 4){
                mscpData.put("拒绝原因",entity.getReComments());
            }
            mscpData.put("被访人",entity.getTname());
            mscpData.put("访客姓名",entity.getName());
            mscpData.put("访客电话",entity.getMobile());
            SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mscpData.put("计划来访时间",dateFm.format(entity.getPgotime()));
            mscpData.put("访问事由",entity.getReason());


            messEntity2.setData(JSONObject.toJSONString(mscpData));
            messEntity2.setCreateTime(new Date());
            messageDao.insert(messEntity2);
        }
    }
}
